package com.atjinggu.jinggumail.ware.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atjinggu.jinggumail.common.enums.BizCodeEnum;
import com.atjinggu.jinggumail.common.to.SkuHasStockVo;
import com.atjinggu.jinggumail.ware.vo.FareVo;
import com.atjinggu.jinggumail.ware.vo.LockStockResult;
import com.atjinggu.jinggumail.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atjinggu.jinggumail.ware.entity.WareSkuEntity;
import com.atjinggu.jinggumail.ware.service.WareSkuService;
import com.atjinggu.jinggumail.common.utils.PageUtils;
import com.atjinggu.jinggumail.common.utils.R;



/**
 * 商品库存
 *
 * @author jingming
 * @email jingming@gmail.com
 * @date 2020-12-13 12:27:45
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wmsWareSkuService;

    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVo vo) {
        try {
          wmsWareSkuService.orderLockStock(vo);
            return R.ok();

        }catch(Exception e){
            return  R.error(BizCodeEnum.STOCK_NOT_ENOUGH.getCode(),BizCodeEnum.STOCK_NOT_ENOUGH.getMessage());
        }
    }




    @GetMapping("/{memberId}/getFare")
    public R getFare(@PathVariable("memberId") String memberId){
        FareVo fareVo =   wmsWareSkuService.getFare(memberId);
        return R.ok().setData(fareVo);
    }

    @PostMapping("/getSkusHasStock")
    public R getSkusHasStock(@RequestBody List<Long> skuIds){
       List<SkuHasStockVo> list =  wmsWareSkuService.getSkusHasStock(skuIds);
        return R.ok().setData(list);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:wmswaresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wmsWareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:wmswaresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wmsWareSku = wmsWareSkuService.getById(id);

        return R.ok().put("wmsWareSku", wmsWareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:wmswaresku:save")
    public R save(@RequestBody WareSkuEntity wmsWareSku){
		wmsWareSkuService.save(wmsWareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:wmswaresku:update")
    public R update(@RequestBody WareSkuEntity wmsWareSku){
		wmsWareSkuService.updateById(wmsWareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:wmswaresku:delete")
    public R delete(@RequestBody Long[] ids){
		wmsWareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
